﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DigitsRecognizer.UI;
using NeuralNetwork;

namespace DigitsRecognizer
{
    public class MainForm : Form
    {
        private string Login;
        private LogOnForm LogOnForm;
        private PatternsForm PatternsForm;
        private TrainingForm TrainingForm;
        private TestForm TestForm;
        private Label LoginLabel, ApplicationName;
        private MainMenu MainMenu;                      // menu główne aplikacji
        private MenuItem TestMI, TrainingMI, PatternsMI, AboutMI, EndMI;     // elementy menu
        private Image Logo;                                     // logo pwr
        private Icon icon;                                      // ikona pwr

        private NeuralNetwork<double> NeuralNetwork;

        public MainForm() 
        {
            this.Height = 400;
            this.Width = 400;
            this.CenterToScreen();
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.Text = "DigitsRecognizer";

            LoginLabel = new Label();
            LoginLabel.Text = "";

            /* inicjalizacja głównego menu */
            MainMenu = new MainMenu();
            TestMI = MainMenu.MenuItems.Add("Test");
            TrainingMI = MainMenu.MenuItems.Add("Trening");
            PatternsMI = MainMenu.MenuItems.Add("Zbiór treningowy");
            AboutMI = MainMenu.MenuItems.Add("O projekcie");
            EndMI = MainMenu.MenuItems.Add("Zakończ");

            ApplicationName = new Label();
            ApplicationName.AutoSize = true;
            ApplicationName.TextAlign = ContentAlignment.TopCenter;
            ApplicationName.Text = "Digits Recognizer";
            ApplicationName.Location = new Point(65, 2);
            ApplicationName.Font = new Font(new FontFamily("Courier"), 18, FontStyle.Bold);

            /* wczytanie loga poprzez odczytanie ścieżki wykonalnej logo_pwr.gif*/
            Logo = System.Drawing.Image.FromFile(@"../../Content/pwr.jpg");
            this.BackgroundImage = Logo;
            this.BackgroundImageLayout = ImageLayout.Center;

            icon = new System.Drawing.Icon(@"../../Content/Icon_pwr.ico");
            this.Icon = icon;

            EventHandler testEH = new EventHandler(OnClickTest);
            TestMI.Click += testEH;

            EventHandler trainingEH = new EventHandler(OnClickTraining);
            TrainingMI.Click += trainingEH;

            EventHandler patternsEH = new EventHandler(OnClickPatterns);
            PatternsMI.Click += patternsEH;

            EventHandler aboutEH = new EventHandler(OnClickAbout);
            AboutMI.Click += aboutEH;

            EventHandler endEH = new EventHandler(OnClickEnd);
            EndMI.Click += endEH;

            this.Controls.Add(ApplicationName);
            this.Menu = MainMenu;
            RunLogOnForm();
        }

        public MainForm(string login) : this()
        {
            Login = login;
        }

        private void RunLogOnForm() 
        {
            LogOnForm = new LogOnForm();
            this.Visible = false;
            this.Enabled = false;
            LogOnForm.Visible = true;
            LogOnForm.TopMost = true;

            /* zarejstrowanie zdarzenia dla SettingsForm */
            LogOnForm.passControl += new LogOnForm.PassControl(PassData);
            LogOnForm.Show();

            this.Visible = true;
            this.Enabled = true;
        }

        /// <summary>
        /// metoda odpowiedzialna za obsługę przyjścia danych z LogOnForm 
        /// odczytuje z tablicy stringów login użytkownika
        /// </summary>
        /// <param name="sender"></param>
        public void PassData(object sender)
        {
            Login = ((String[])sender)[0];

            if (Login.Equals("-1")) 
            {
                this.Close();
            }

            LoginLabel.Text = Login;
            this.Text = string.Format("DigitsRecognizer | User : {0}", Login);
            this.Refresh();
        }

        public void OnClickAbout(object sender, EventArgs args)
        {
            MessageBox.Show("Sztuczna inteligencja w badaniach operacyjnych - Projekt\n\nProwadzący:\nDr inż. Zbigniew Buchalski\n\nWykonał:\nPiotr Kłys 171168\n\nV INF \nRok akademicki:  2012/2013\n\nLicencja: Powszechna Licencja Publiczna GNU\nWrocław 2013r.", "O projekcie", MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        public void OnClickEnd(object sender, EventArgs args)
        {
            if (MessageBox.Show("Czy na pewno chcesz zakończyć działanie aplikacji?", "Zakończ", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        public void OnClickPatterns(object sender, EventArgs args)
        {
            PatternsForm = new PatternsForm(Login);

            this.Visible = false;
            this.Enabled = false;
            PatternsForm.Visible = true;
            PatternsForm.TopMost = true;

            PatternsForm.Show();

            this.Visible = true;
            this.Enabled = true;
        }

        public void OnClickTraining(object sender, EventArgs args)
        {
            TrainingForm = new TrainingForm(Login);

            this.Visible = false;
            this.Enabled = false;
            TrainingForm.Visible = true;
            TrainingForm.TopMost = true;

            TrainingForm.Show();

            this.Visible = true;
            this.Enabled = true;
        }

        public void OnClickTest(object sender, EventArgs args)
        {
            TestForm = new TestForm(Login);

            this.Visible = false;
            this.Enabled = false;
            TestForm.Visible = true;
            TestForm.TopMost = true;

            TestForm.Show();

            this.Visible = true;
            this.Enabled = true;
        }
    }
}
